//
//  testControlViewViewController.m
//  testControlView
//
//  Created by Le Thanh Hai on 10/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "testControlViewViewController.h"

@implementation testControlViewViewController

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
	_controlView = [[MenuViewController alloc] initWithFrame:CGRectMake(0.0f, self.view.frame.size.height-[MenuViewController barHeight], 
																 self.view.frame.size.width, 460.0f)];
    [self.view addSubview:_controlView];
    
    // Add controls to control view
	CGRect controlFrame;
	int borderWidth = 5;
	float controlSpacing, currSpacing = [MenuViewController barHeight];
	SEL controlSelectors[] = { @selector(changeCompression:), @selector(changeMipmap:), @selector(changeFilter:) };
	int numControls = sizeof(controlSelectors)/sizeof(controlSelectors[0]);
	UISegmentedControl *controls[numControls];
	int numDivs = 2 + numControls - 1;
    
    NSArray *compressionControlText = [NSArray arrayWithObjects:@"None", @"4BPP", @"2BPP", nil];

	
	NSArray *mipmapFilterControlText = [NSArray arrayWithObjects:@"Off", @"Nearest", @"Linear", nil];
	
	NSArray *textureFilterControlText = [NSArray arrayWithObjects:@"Nearest", @"Linear", @"Super", nil];

    
	NSArray *controlText = [NSArray arrayWithObjects:compressionControlText, mipmapFilterControlText, textureFilterControlText, nil];
	NSArray *labelText = [NSArray arrayWithObjects:@"Compression", @"Mipmap Filter", @"Texture Filter", nil];
	UILabel *label;
    
    for (int i=0; i < numControls; i++)
	{			
		controls[i] = [[[UISegmentedControl alloc] initWithItems:[controlText objectAtIndex:i]] autorelease];
		[controls[i] setSegmentedControlStyle:UISegmentedControlStyleBar];
		[controls[i] setSelectedSegmentIndex:0];
		controlFrame = controls[i].frame;
		
		if (i == 0)
			currSpacing += controlSpacing = floorf(([_controlView contentHeight] - (controlFrame.size.height * numControls))/numDivs);
		else
			currSpacing += controlSpacing + controls[i].frame.size.height;
		
		label = [[[UILabel alloc] initWithFrame:CGRectMake(borderWidth,
														   currSpacing,
														   _controlView.frame.size.width - (2 * borderWidth) - controls[i].frame.size.width,
														   controls[i].frame.size.height)] autorelease];
		label.text = [labelText objectAtIndex:i];
		label.textColor = [UIColor whiteColor];
		label.backgroundColor = [UIColor clearColor];
		[_controlView addSubview:label];
		
		controlFrame.origin.x = _controlView.frame.size.width - controlFrame.size.width - borderWidth;
		controlFrame.origin.y = currSpacing;
		[controls[i] setFrame:controlFrame];
		controls[i].tintColor = [UIColor colorWithRed:0.40f green:0.40f blue:0.40f alpha:0.65f];
		controls[i].tag = i;
		[controls[i] addTarget:self action:controlSelectors[i] forControlEvents:UIControlEventValueChanged];
		[_controlView addSubview:controls[i]];
	}
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
